protocol TextRepresentable {
    var texttualDesc: String { get }
}

extension Dice: TextRepresentable {
    var texttualDesc: String {
        return "A \(sides)-sided dice"
    }
}

class Dice {
    var sides: Int
    init(sides: Int) {
        self.sides = sides
    }
}

var d = Dice(sides: 3)
print(d.texttualDesc)

print("------------ 有条件的遵循协议 ------------")
// 数组和元素都遵循协议
extension Array: TextRepresentable where Element: TextRepresentable {
    var texttualDesc: String {
        let itemsAsText = self.map{ $0.texttualDesc }
        return "[" + itemsAsText.joined(separator: ",") + "]"
    }
}
let myDice = [Dice(sides: 2), Dice(sides: 1), Dice(sides: 5)]
print(myDice.texttualDesc)

